//
//  ViewController.m
//  runtime创建类
//
//  Created by webplus on 17/8/30.
//  Copyright © 2017年 sanyi. All rights reserved.
//

#import "ViewController.h"
#import <objc/runtime.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建类
    const char *className;
    className = [@"Student" UTF8String];
    Class studentClass = objc_getClass(className);
    if (!studentClass) {
        Class superClass = [NSObject class];
        studentClass = objc_allocateClassPair(superClass, className, 0);
    }else{
        return;
    }
    //创建实例变量（成员变量）
    class_addIvar(studentClass, "_stuName", sizeof(NSString *), 0, "@");
    class_addMethod([studentClass class], @selector(say:), (IMP)say, "v@:");
    //创建属性
    NSString *propertyName = @"stuSex";
    //关联实例变量
     class_addIvar(studentClass, [propertyName UTF8String], sizeof(NSString *), 0, "@");
    objc_property_attribute_t type = {"T","@\"NSString\""};
    objc_property_attribute_t ownership = {"C","copy"};
    objc_property_attribute_t backingivar = {"V",[propertyName UTF8String]};
    objc_property_attribute_t attrs[] = { type, ownership, backingivar };
    BOOL isOK = class_addProperty(studentClass, [propertyName UTF8String], attrs, 3);
    
    objc_registerClassPair(studentClass);
    //创建存取方法（关联实例变量后，不需要，自动合成）
    class_addMethod(studentClass,@selector(stuSex), (IMP)Getter, "@@:");
    class_addMethod(studentClass,@selector(setStuSex:), (IMP)Setter, "v@:@");
    
    id student = [[studentClass alloc]init];
    //调取方法
    
    [student setValue:@"张三" forKey:@"stuName"];
    NSLog(@"%@",[student valueForKey:@"stuName"]);
    [student say:[student valueForKey:@"stuName"]];
    
    [student setValue:@"男" forKey:@"stuSex"];
    NSLog(@"%@",[student valueForKey:@"stuSex"]);
    
    [student setStuSex:@"男"];
    NSLog(@"%@",[student stuSex]);
}

- (void)say:(NSString *)aString{
    
}

void say(id self,SEL _cmd,NSString *aString){
    NSLog(@"你好%@",aString);
}

- (void)setStuSex:(NSString *)stuSex
{
    
}
- (NSString *)stuSex
{
    return nil;
}

NSString * Getter(id self1,SEL _cmd1){
    NSString * var=NSStringFromSelector(_cmd1);
    Ivar ivar=class_getInstanceVariable([self1 class], [var cStringUsingEncoding:NSUTF8StringEncoding]);
    return object_getIvar(self1, ivar);
}
void Setter(id self1,SEL _cmd1,NSString* newObject){
    NSString * var=[NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];
    NSString * head=[var substringWithRange:NSMakeRange(0, 1)];
    head=[head lowercaseString];
    var=[var stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:head];
    var=[var stringByReplacingCharactersInRange:NSMakeRange([var length]-1, 1) withString:@""];
    Ivar ivar=class_getInstanceVariable([self1 class], [var cStringUsingEncoding:NSUTF8StringEncoding]);
    id oldName=object_getIvar(self1, ivar);
    if (oldName!=newObject) {
        object_setIvar(self1, ivar, [newObject copy]);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
